<template>
    <div>
        <Button v-if="showBackBtn" class="goBack" type="primary" @click="$router.back()"><Icon type="ios-arrow-back" /> 返回  </Button>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-main">
                    <Form ref="formCustom" :model="formCustom" :rules="ruleCustom" :label-width="80" >
                        <Row>
                            <Col span="7">
                                <Form-item label="人员类型" prop="type">
                                    <Select v-model="formCustom.type" style="width:100%"
                                            placeholder="选择人员类型">
                                        <Option v-for="item in peopleTypeList" :key="item.value" :value="item.value">{{item.label}}</Option>
                                    </Select>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-item label="人员数量" prop="num" >
                                    <i-input type="number" number v-model="formCustom.num" placeholder="请输入人员数量"><span slot="append">人</span></i-input>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-Item label="是否发热" prop="fever">
                                    <RadioGroup v-model="formCustom.fever" >
                                        <Radio label="YES">是</Radio>
                                        <Radio label="NO">否</Radio>
                                    </RadioGroup>
                                </Form-Item>
                            </Col>
                            <Col span="7" v-if="formCustom.fever==='YES'">
                                <Form-Item  label="发热人数" prop="fever_num">
                                    <i-input type="number" number v-model="formCustom.fever_num" placeholder="请输入发热人数"><span slot="append">人</span></i-input>
                                </Form-Item>
                            </Col>
                            <Col span="7" >
                                <Form-item label="上报日期" prop="imp_date">
                                    <DatePicker @on-change="dateChange" :options="options1"   :value="formCustom.imp_date" format="yyyy-MM-dd"
                                                type="date" placeholder="选择上报日期"></DatePicker>
                                </Form-item>
                            </Col>
                        </Row>
                    </Form>
                    <div class="line-button" v-if="!showDetail">
                        <Button type="primary" @click="validateForm('formCustom')">保存</Button>
                    </div>
                </div>
            </div>
        </Card>
        <Modal
                v-model="modal"
                title="确认操作"
                width="20"
                @on-ok="handleSubmit"
        >
            <div style="text-align:center">
                <p>是否确认保存!</p>
            </div>
        </Modal>

    </div>
</template>

<script>
import { insert, getById, update, getPeopleType } from '@/api/outbreak-management/key-population/personnel'

export default {
  data () {
    const validateInt = (rule, value, callback) => {
      if (value % 1 !== 0) {
        callback(new Error('请输入整数'))
      } else {
        callback()
      }
    }
    return {
      options1: { // 不可选今天之后的日期
        disabledDate (date) {
          return date && date.valueOf() > Date.now()
        }
      },
      peopleTypeList: [],
      // 弹窗控制
      modal: false,
      // 控制保存按钮显隐
      showDetail: false,
      // 控制返回按钮显隐
      showBackBtn: false,
      formCustom: {
        type: '',
        num: 0,
        fever: '',
        imp_date: '',
        fever_num: 0
      },
      ruleCustom: {
        type: [
          { required: true, message: '人员类型必选', trigger: 'change' }
        ],
        num: [
          { required: true, type: 'number', message: '人员数量必填', trigger: 'blur' },
          { min: 0, max: 99999999, type: 'number', message: '人员数量范围0-99999999', trigger: 'blur' },
          { validator: validateInt, trigger: 'blur' }
        ],
        fever_num: [
          { required: true, type: 'number', message: '发热人数必填', trigger: 'blur' },
          { min: 0, max: 99999999, type: 'number', message: '发热人数范围0-99999999', trigger: 'blur' },
          { validator: validateInt, trigger: 'blur' }
        ],
        fever: [
          { required: true, message: '是否发热必选', trigger: 'change' }
        ],
        imp_date: [
          { required: true, message: '上报日期必选', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    validateForm (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.modal = true
        } else {
          this.$Message.error('表单验证失败,请检查后再试!')
        }
      })
    },

    /**
             * 日期格式化
             */
    dateChange (date) {
      this.formCustom.imp_date = date
    },
    handleSubmit () {
      let requestData = this.formCustom
      if (this.formCustom.fever === 'NO') {
        this.formCustom.fever_num = 0
      }
      if (requestData.id) {
        update(requestData).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error('保存失败!')
          }
        })
      } else {
        insert(requestData).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error('保存失败!')
          }
        })
      }
    },
    isEditStatus () {
      if (this.$route.query && this.$route.query.id) {
        if (this.$route.query.showDetail) {
          this.showDetail = true
        }
        this.showBackBtn = true
        this.getEditData(this.$route.query.id)
      }
    },
    getEditData (id) {
      getById({ id }).then(({ data }) => {
        let { errcode, data: res } = data
        if (errcode === 0) {
          this.formCustom = res
          this.formCustom.imp_date = this.$common.moment(res.imp_date).format('YYYY-MM-DD')
        }
        console.log(data)
      })
    }
  },
  created () {
    this.isEditStatus()
    getPeopleType().then(({ data }) => {
      let { errcode, data: res } = data
      if (errcode === 0) {
        this.peopleTypeList = res
      }
    })
  },
  mounted () {
  }
}
</script>

<style scoped>

</style>
